<?php
namespace App\Org;
define('AccessKeyId', config('app.AliyunAccessKeyId'));
define('AccessKeySecret', config('app.AliyunAccessKeySecret'));
date_default_timezone_set('UTC');

class Dns
{
    const DomainName='cadj.cn';
    const IP='114.55.3.34';

    /**
     *  生成请求签名
     */
    public static function getSign($add)
    {
        $nonce = time() . rand(11111, 99999);
        $time = date('Y-m-d') . 'T' . date('H:i:s') . 'Z';
        $data = array(
            'AccessKeyId' => AccessKeyId,
            'Format' => 'json',
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => $nonce,
            'SignatureVersion' => '1.0',
            'Timestamp' => $time,
            'Version' => '2015-01-09',
            'Lang' => 'cn',
        );
        $data = array_merge($data, $add);
        ksort($data);
        $format = http_build_query($data);
        $signUrl = 'GET&%2F&' . urlencode($format);
        $sign = urlencode(base64_encode(hash_hmac('sha1', $signUrl, AccessKeySecret . '&', true)));
        return array('url' => $format, 'sign' => $sign);
    }

    //根据id获取记录信息
    public static function DescribeDomainRecordInfo($id)
    {
        $sign_add=[
            'Action' => 'DescribeDomainRecordInfo',
            'RecordId' => $id,
        ];
        $sign = self::getSign($sign_add);
        $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
        $res = json_decode(self::httpRequest($url));
        if (!isset($res->RecordId)) {
            throw new \Exception('记录不存在',-1);
        }
        return $res;
    }


    /**
     *  获取域名解析记录
     */
    public static function getDomainRecords($add)
    {
        $sign_add=[
            'Action' => 'DescribeDomainRecords',
            'DomainName' => self::DomainName,
        ];
        $sign_add=array_merge($sign_add,$add);
        $sign = self::getSign($sign_add);
        $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
        $data = json_decode(self::httpRequest($url));
        if (isset($data->Code)) {
            throw new \Exception($data->Message,-1);
        }
        $data_res=[
            'list'=>$data->DomainRecords->Record,
            'count'=>$data->TotalCount,
        ];
        return $data_res;
    }

    /**
     *  更新域名解析记录
     */
    public static function UpdateDomainRecord($add)
    {
        $sign_add=[
            'Action' => 'UpdateDomainRecord',
            'DomainName' => self::DomainName,
            'Type' => 'A',
            'Value' => self::IP,
        ];
        $sign_add=array_merge($sign_add,$add);
        $sign = self::getSign($sign_add);
        $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
        $res= json_decode(self::httpRequest($url));
        if(isset($res->Code))
        {
            throw new \Exception($res->Message,-1);
        }
        return $res;
    }

    /**
     *  添加域名解析记录
     */
    public static function addDomainRecords($add)
    {
        $sign_add=[
            'Action' => 'AddDomainRecord',
            'DomainName' => self::DomainName,
            'Type' => 'A',
            'Value' => self::IP,
        ];
        $sign_add=array_merge($sign_add,$add);
        $sign = self::getSign($sign_add);
        $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
        $res= json_decode(self::httpRequest($url));
        if(isset($res->Code))
        {
            throw new \Exception($res->Message,-1);
        }
        return $res;
    }

    /**
     *  更新域名解析记录
     */
    public static function deleteDomainRecord($add)
    {
        $sign_add=[
            'Action' => 'DeleteDomainRecord',
        ];
        $sign_add=array_merge($sign_add,$add);
        $sign = self::getSign($sign_add);
        $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
        $res= json_decode(self::httpRequest($url));
        if(isset($res->Code))
        {
            throw new \Exception($res->Message,-1);
        }
        return $res;
    }

    public static function UpdateDomainRecordRemark($add)
    {
        $sign_add=[
            'Action' => 'UpdateDomainRecordRemark',
        ];
        $sign_add=array_merge($sign_add,$add);
        $sign = self::getSign($sign_add);
        $url = 'https://alidns.aliyuncs.com/?' . $sign['url'] . '&Signature=' . $sign['sign'];
        $res= json_decode(self::httpRequest($url));
        if(!isset($res->RequestId))
        {
            throw new \Exception($res->Message,-1);
        }
        return $res;
    }

    /**
     *  发起 HTTP 请求
     */
    public static function httpRequest($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($curl);
        $error = curl_error($curl);
        curl_close($curl);
        if ($error) {
            return $error;
        } else {
            return $data;
        }
    }

}

